package com.eckey.lab.controller;

import com.alibaba.fastjson.JSON;
import com.eckey.lab.entity.User;
import com.eckey.lab.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/findOrderByUserId")
    public String findOrderByUserId() {
        HashMap<String, Object> maps = new HashMap<String, Object>();
        maps.put("name", "张三");
        maps.put("age", 19);
        log.info("返回用户信息：{}", maps);
        return JSON.toJSONString(maps);
    }

    @GetMapping("/findById")
    public String getUserById(@RequestParam("id") Integer id) {
        HashMap<String, Object> results = new HashMap<String, Object>();
        User user = userService.queryById(id);
        results.put("data", JSON.toJSONString(user));
        log.info("user:{}", JSON.toJSONString(user));
        results.put("code", "0");
        return JSON.toJSONString(results);
    }


    @GetMapping("/deduct")
    public String deduct(@RequestParam("userId") Integer userId, @RequestParam("totalPrice") Integer totalPrice) {
        HashMap<String, Object> results = new HashMap<String, Object>();
        userService.deduct(userId, totalPrice);
        results.put("data", "success");
        results.put("code", "0");
        return JSON.toJSONString(results);
    }

}
